﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Xml.Serialization;
using RaisingForce.Raise.Language.Rules;

namespace RaisingForce.Raise.Language.Model.Rules
{
    [Serializable]
    [XmlType(TypeName = "RuleSet")]
    sealed public class RuleSetData
    {
        #region Constructors

        public RuleSetData()
        {
            this.name = RuleSetData.DefaultName;
            // this.rules = null;
        }

        public RuleSetData(string name)
        {
            if (!KnowledgeRules.NameIsValid(name))
            {
                throw new ArgumentException("name");
            }
            Contract.EndContractBlock();
            this.name = name;
            // this.rules = null
        }

        public RuleSetData(string name, IList<RuleData> rules)
        {
            if (!KnowledgeRules.NameIsValid(name))
            {
                throw new ArgumentException("name");
            }
            Contract.EndContractBlock();
            this.name = name;
            this.rules = new RuleDataCollection(rules);
        }

        internal RuleSetData(string name, RuleDataCollection rules)
        {
            Contract.Requires(KnowledgeRules.NameIsValid(name));
            this.name = name;
            this.rules = rules;
        }

        #endregion

        #region Private Declarations

        private string name;

        private RuleDataCollection rules;

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(KnowledgeRules.NameIsValid(this.name));
        }

        #endregion

        #region Constants

        public const string DefaultName = "RuleSet1";

        #endregion

        #region Properties

        [XmlAttribute(AttributeName = "Name")]
        public string Name
        {
            get
            {
                Contract.Ensures(KnowledgeRules.NameIsValid(Contract.Result<string>()));
                return this.name;
            }
            set
            {
                if (!KnowledgeRules.NameIsValid(value))
                {
                    throw new ArgumentException("Invalid Name property value.", "Name");
                }
                Contract.EndContractBlock();
                this.name = value;
            }
        }

        [XmlElement(ElementName = "Rules")]
        public RuleDataCollection Rules
        {
            get
            {
                Contract.Ensures(Contract.Result<RuleDataCollection>() != null);
                if (this.rules == null)
                {
                    this.rules = new RuleDataCollection();
                }
                return this.rules;
            }
            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("Rules");
                }
                Contract.EndContractBlock();
                this.rules = value;
            }
        }

        #endregion
    }
}
